package ai

import (
	openai2 "chatgpt/ai/openai"
	"chatgpt/helper"
	"chatgpt/helper/code"
	"github.com/gin-gonic/gin"
	"github.com/slclub/go-tips"
)

func Chat(ctx *gin.Context, quest string, m map[string]any, call func() string) (code.CODENO, *helper.ResponseDesc) {
	if StreamCheck(m["stream"]) {
		response := helper.ResponseDesc{}
		openai2.RemoteStream(ctx.Writer, openai2.OPENAI_CHAT, map[string]any{
			"q":      quest,
			"model":  tips.String(m["model"]),
			"smodel": tips.String(m["smodel"]),
			"unsafe": helper.UNSAFE_MOD,
			"stream": m["stream"],
		}, &response)
		return code.SUCCESS, &response
	}
	mcode, replay := openai2.RemoteApi(openai2.OPENAI_CHAT, quest, map[string]string{
		"model":  tips.String(m["model"]),
		"smodel": tips.String(m["smodel"]),
		"stops":  tips.String(m["stops"]),
		"unsafe": helper.UNSAFE_MOD,
	})
	helper.Echo(ctx, int(mcode), replay.Data)
	return mcode, replay
}
